﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MyFirstSolution.Common
{
    /// <summary>
    /// 应用程序的配置参数类
    /// </summary>
    public class MyConfig
    {
        /// <summary>
        /// 配置文件的名称(默认位于程序的安装目录下--也可以由客户端程序指定路径)
        /// </summary>
        private static string ConfigFileName = "MyConfig.INI";

        /// <summary>
        /// INI配置文件读写操作管理对象
        /// </summary>
        private IniFile MyCFG = null;

        private bool autologin;
        /// <summary>
        /// 用户是否自动登录
        /// </summary>
        public bool AutoLogin
        {
            get { return autologin; }
            set
            {
                MyCFG.IniWriteValue("Login", "AutoLogin", value.ToString());
                autologin = value;
            }
        }

        private string loginname;
        /// <summary>
        /// 当前登录用户的帐号
        /// </summary>
        public string LoginName
        {
            get { return loginname; }
            set
            {
                if (value.Trim().Length > 0)//用户名称不允许为空
                {
                    loginname = value.Trim();
                    MyCFG.IniWriteValue("Login", "LoginName", loginname);
                }
            }
        }

        private string loginpassword;
        /// <summary>
        /// 当前登录用户的密码
        /// </summary>
        public string LoginPassword
        {
            get { return loginpassword; }
            set
            {
                if (value.Trim().Length > 0) //登录密码不允许为空
                {
                    loginpassword = value.Trim();
                    MyCFG.IniWriteValue("Login", "LoginPassword", CodeProcess.EncodeBase64(loginpassword));
                }
            }
        }

        private string connectstr;
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectString
        {
            get { return connectstr; }
            set
            {
                if (value.Trim().Length > 0)
                {
                    connectstr = value.Trim();
                    MyCFG.IniWriteValue("Database", "ConnectString", connectstr);
                }
            }
        }

        /// <summary>
        /// 构造配置文件管理类的对象
        /// </summary>
        /// <returns>是否构建成功?</returns>
        private bool ConstructObj()
        {
            if (!File.Exists(ConfigFileName))
                return false;

            try
            {
                MyCFG = new IniFile(ConfigFileName);

                autologin = Convert.ToBoolean(MyCFG.IniReadValue("Login", "AutoLogin"));
                loginname = MyCFG.IniReadValue("Login", "LoginName").Trim();
                loginpassword = MyCFG.IniReadValue("Login", "LoginPassword").Trim();
                if (loginpassword.Length == 0) //密码不能为空,碰到空密码自动设置为默认密码--"123"
                    loginpassword = "123";
                else
                    loginpassword = CodeProcess.DecodeBase64(loginpassword);
                connectstr = MyCFG.IniReadValue("Database", "ConnectString").ToString();
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 系统配置参数对象的构造--自动读取配置文件的内容--当前程序所在路径的默认配置文件
        /// </summary>
        public MyConfig()
        {
            ConfigFileName = Environment.CurrentDirectory + "\\MyConfig.INI";

            ConstructObj();
        }

        /// <summary>
        /// 系统配置参数对象的构造--自动读取配置文件的内容--指定打开的配置文件
        /// </summary>
        /// <param name="fileName">要打开的配置文件的全路径名称</param>
        public MyConfig(string fileName)
        {
            ConfigFileName = fileName;

            ConstructObj();
        }
    }
}
